package com.corner.presenter.imp;

import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.helper.InfoHelper;
import com.corner.presenter.IShowSinglePresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.view.IShowSingleView;
import com.corner.utils.DateFormatUtil;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by Administrator on 2015/7/15，17.01.
 * 描述：
 */
public class ShowSinglePresenter extends BasePresenter<IShowSingleView> implements IShowSinglePresenter {

    private boolean isLoading;
    private long lastRefreshTime;

    @Override
    public void requestInfo(Long infoId) {
        if (isLoading) {
            return;
        }
        if (DateFormatUtil.getCurrentTimeInMills() - lastRefreshTime < 3000) {
            view.requestComplete();
            return;
        }

        isLoading = true;
        lastRefreshTime = DateFormatUtil.getCurrentTimeInMills();
        InfoHelper.getInfo(infoId).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Info>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.requestInfoError(e.getMessage());
                    view.requestComplete();
                }
                isLoading = false;
            }

            @Override
            public void onNext(Info info) {
                if (view != null) {
                    view.showInfo(info);
                    view.requestComplete();
                }
                isLoading = false;
            }
        });
    }
}
